/**
 * Created by zhanglihe on 15/11/10.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'customcatethree',
                condition: {limit: 10, offset: 0,cate_id:cate_id},
                model: {
                    url: _global.url.api + 'customcatethree',
                    event: {
                        '.item-remove:click': 'remove',
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;console.log(this)
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                model.data.cate_id =cate_id;
                                if(model.del()){
                                    _this.collection.fetch();
                                    _this.move();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    }

                },
                callback: function() {
                    loaded();
                    _this.move();
                }
            });
            this.collection.fetch();
        },move:function(){
            $("#data-list div").find('a.up').show();
            $("#data-list div").find("a.down").show();
            $("#data-list div:eq(0)").find("a.up").hide();
            $("#data-list div:last").find("a.down").hide();
        },
        editlistorder:function(){
            var _this = this;
            var cate_ids = new Array();
            $('.J_CheckItem').each(function(){
                cate_ids.push($(this).val());
            });
            _this.data.cate_ids =cate_ids.join(',');
            _this.data.cate_id =cate_id;
            if(new Model({
                    url: _global.url.api + 'customcatethree_set',
                    data: _this.data
                }).put()) {
                _this.collection.fetch();
            };
        },
        event: function() {
            this.data = {};
            var _this = this;
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
            $('body').on('click', '.up', function(e){
                e.stopPropagation();
                $(this).parents("tr").insertBefore($(this).parents("tr").prev());
                _this.move();
                _this.editlistorder();
                return false;
            });
            $('body').on('click', '.down', function(e){
                e.stopPropagation();
                $(this).parents("tr").insertAfter($(this).parents("tr").next());
                _this.move();
                _this.editlistorder();
                return false;
            });

            $("#btn-add").on("click",function(e){
                e.preventDefault();
                var html=template('add-tpl');
                dialog({
                    'content': html,
                    ok: function() {
                        _this.data.cate_id=cate_id;
                        var cate_ids =new Array();
                        $("input[name='cate_id']").each(function(e){
                            cate_ids.push($(this).val());
                        });
                        _this.data.cate_ids =cate_ids.join(',');
                        new Model({
                            url: _global.url.api + 'customcatebind',
                            data:_this.data
                        }).post();
                        _this.collection.fetch();
                        _this.move();
                    }, cancel: function() {}
                }).show();
                goods_list();
                $("#btn-searchs").on('click',function(e){
                    var keyword =$("#keywords").val();
                    goods_list(keyword)
                });
            });
        }


    };

    var goods_list=function(keyword){
        this.collection = new Collection({
            container: 'data-list_goods',
            template: 'goods_check-tpl',
            page_dom:'paginations',
            url: _global.url.api + 'cunstom_cate_level',
            condition: {keyword:keyword,limit: 5, offset: 0,cate_id:cate_id},
            model: {
                event: {
                    '.item-select:click': 'goodsnice_set',
                    '.item-select_redy:click':'goodsnice_qx'
                },
                goodsnice_set: function(e) {
                    e.preventDefault();
                    var id =this.data.id;
                    $(".item-selects"+id+"").show();
                    $(".item-select"+id+"").hide();
                    $("#reason_list").append("<div id='intro"+id+"' style='width:800px;height:30px;line-height:30px;margin-bottom:2px;margin-left:10px;'><input type='hidden' value='"+id+"' name='cate_id'><span style='float: left;'>"+id+"-"+this.data.name+"</span><a href='#' class='remove"+id+"' style='float: right;margin-right:20px;'><span class='glyphicon glyphicon-remove'></span></a></div>");
                    $(".remove"+id+"").on('click',function(){
                        $("#intro"+id+"").remove();
                        $(".item-selects"+id+"").hide();
                        $(".item-select"+id+"").show();
                    })
                },
                goodsnice_qx: function(e) {
                    e.preventDefault();
                    var id =this.data.id;
                    $(".item-selects"+id+"").hide();
                    $(".item-select"+id+"").show();
                    $("#intro"+id+"").remove();
                }

            },
            callback: function() {
                loaded();
                $("input[name='cate_id']").each(function(){
                    var id =$(this).val();
                    $(".item-selects"+id+"").show();
                    $(".item-select"+id+"").hide();
                });
            }
        });
        this.collection.reload();
    }
    new index();

});